package org.iabako.client.ui.page.business.sale;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.config.page.PageConfigSearchEnum;
import org.iabako.client.config.page.PageEnum;
import org.iabako.client.navigation.NavigationController;
import org.iabako.client.tools.DefaultSuggestHandler;
import org.iabako.client.ui.form.business.sale.SaleSearchForm;
import org.iabako.client.ui.layout.field.Button;
import org.iabako.client.ui.layout.field.LabelText;
import org.iabako.client.ui.layout.widget.datatable.DataTable;
import org.iabako.client.user.AuthenticateUserProvider;
import org.iabako.shared.entity.enumeration.TrackingType;
import org.iabako.shared.iservice.business.ClientServiceAsync;
import org.iabako.shared.iservice.business.QuoteServiceAsync;
import org.iabako.shared.iservice.generic.CRUDServiceAsync;
import org.iabako.shared.iservice.generic.SearchServiceAsync;
import org.iabako.shared.iservice.generic.SuggestServiceAsync;

/**
 * Created by jose on 12/07/15.
 */
public class QuoteSearch extends SaleSearch {

    interface MyUiBinder extends UiBinder<HTMLPanel, QuoteSearch> {
    }

    private static final MyUiBinder binder = GWT.create(MyUiBinder.class);

    @UiField
    HTMLPanel saleSearchPanel;
    @UiField
    HTMLPanel resultsPanel;

    public QuoteSearch() {
        super();
        Button createSale = new Button("pageactionbutton", "quote_make_title");
        page.actionButtons.add(createSale);

        createSale.getButton().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                NavigationController.getInstance().goTo(PageEnum.QuoteMake);
            }
        });
        LabelText comeBackToSales = new LabelText("pageactionlink");
        comeBackToSales.setStyleClass("linklabel");
        comeBackToSales.setLabel("quote_come_back_to_sales");
        comeBackToSales.setClassIcon("gobackicon");

        page.actionButtons.add(comeBackToSales);
        comeBackToSales.getLabel().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                NavigationController.getInstance().goTo(PageEnum.SaleSearch);
            }
        });
    }

    @Override
    protected Composite getSearchForm(DataTable dataTable) {
        if (saleSearchForm == null) {
            saleSearchForm = new SaleSearchForm(new DefaultSuggestHandler(getSuggestServiceAsync()), dataTable);
            saleSearchForm.activateQuoteMode();
        }
        return saleSearchForm;
    }

    @Override
    protected void dataTableRemoveCustomAction() {
        AuthenticateUserProvider.getTrackingPanel().refresh(TrackingType.quoteDelete);
    }

    @Override
    protected SearchServiceAsync getSearchServiceAsync() {
        return QuoteServiceAsync.Util.getInstance();
    }

    @Override
    protected CRUDServiceAsync getCRUDServiceAsync() {
        return QuoteServiceAsync.Util.getInstance();
    }

    @Override
    protected SuggestServiceAsync getSuggestServiceAsync() {
        return ClientServiceAsync.Util.getInstance();
    }

    @Override
    protected PageConfigSearchEnum getSearchPageConfigEnum() {
        return PageConfigSearchEnum.QuoteSearch;
    }

    @Override
    protected PageEnum getModificationPage() {
        return PageEnum.QuoteModify;
    }
}
